package com.tjdr.his.drugstore.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tjdr.his.common.result.HttpResp;
import com.tjdr.his.common.result.ResultCode;
import com.tjdr.his.domain.entity.GiveBackRecords;
import com.tjdr.his.drugstore.service.IGiveBackRecordsService;
import com.tjdr.his.drugstore.service.IPrescriptionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @AUTHOR 张三
 * @DATE 2024/10/10 14:45
 * @DESCRIBER 发药模块中 待发药患者的信息
 */
@RestController
@CrossOrigin
@RequestMapping("/api/prescription")
@Slf4j
public class PrescriptionController {
    @Autowired
    private IPrescriptionService service;
    @Autowired
    private IGiveBackRecordsService iGiveBackRecordsService;

    /**
     * 发药接口
     * @param id 处方Id
     * @param drugCode 药品编码
     * @param drugNumber 发药品的数量
     * @return
     */
    @PutMapping("givePresById/{id}/{drugCode}/{drugNumber}/{drugPrice}/{drugId}/{employeeId}/{registerId}")
    public HttpResp givePresById(@PathVariable double drugPrice, @PathVariable Integer drugId,@PathVariable Integer employeeId,@PathVariable Integer registerId, @PathVariable Integer id , @PathVariable String drugCode, @PathVariable Integer drugNumber){
        log.debug("updatePresStateById id:{},drugCode:{},drugNumber:{}",id,drugCode,drugNumber);
        boolean result = service.givePresById(id,"已发药",drugCode,drugNumber);
        //存储交易记录
        GiveBackRecords giveBackRecords = new GiveBackRecords();
        giveBackRecords.setNumber(drugNumber);
        giveBackRecords.setOperatorState("已发药");
        giveBackRecords.setDate(new Date());
        giveBackRecords.setDrugId(drugId);
        giveBackRecords.setEmployeeId(employeeId);
        giveBackRecords.setRegisterId(registerId);
        giveBackRecords.setCost(BigDecimal.valueOf(drugPrice*drugNumber));
        iGiveBackRecordsService.saveGiveBackRecords(giveBackRecords);
        HttpResp httpResp = new HttpResp();
        httpResp.setDate(new Date());
        httpResp.setResults(result);
        httpResp.setResultCode(ResultCode.OPTION_SUCCESS);
        log.debug("{}",httpResp);
        return httpResp;
    }

    /**
     * 退药接口

     * @return
     */
    @PutMapping("repercussionDrugById")
    public HttpResp repercussionDrugById(@RequestBody Map<String,String > map){
//        @PathVariable Integer id,@PathVariable String drugCode,Integer drugNumber
        log.debug("repercussionDrugById map:",map);
        int id = Integer.valueOf(map.get("id"));
        String drugCode = map.get("drugCode");
        Integer drugNumber = Integer.valueOf(map.get("drugNumber"));
        int drugId = Integer.valueOf(map.get("drugId"));
        int employeeId = Integer.valueOf(map.get("employeeId"));
        int registerId = Integer.valueOf(map.get("registerId"));
        double drugPrice = Double.valueOf(map.get("drugPrice"));
        boolean result = service.repercussionDrugById(id,drugCode,drugNumber);
        //存储交易记录
        GiveBackRecords giveBackRecords = new GiveBackRecords();
        giveBackRecords.setNumber(drugNumber);
        giveBackRecords.setOperatorState("已退药");
        giveBackRecords.setDate(new Date());
        giveBackRecords.setDrugId(drugId);
        giveBackRecords.setEmployeeId(employeeId);
        giveBackRecords.setRegisterId(registerId);
        giveBackRecords.setCost(BigDecimal.valueOf(drugPrice*drugNumber));
        iGiveBackRecordsService.saveGiveBackRecords(giveBackRecords);
        HttpResp httpResp = new HttpResp();
        httpResp.setDate(new Date());
        httpResp.setResults(result);
        httpResp.setResultCode(ResultCode.OPTION_SUCCESS);
        log.debug("{}",httpResp);
        return httpResp;
    }

    /**
     * 显示药品接口
     * @param id
     * @param pageSize
     * @param currentPage
     * @param drugState
     * @return
     */
    @GetMapping("getPrescriptionByRegisterId/{id}/{pageSize}/{page}")
    public HttpResp getPrescriptionByRegisterId(@PathVariable int id,@PathVariable int pageSize,@PathVariable(name = "page") int currentPage, @RequestParam(required = false,defaultValue = "已缴费") String drugState){
        log.debug("getPrescriptionById：{},{},{}",id,pageSize,currentPage);
        IPage page = service.getRegisterPresDrug(id,pageSize,currentPage,drugState);
        Map<String,Object> map = new HashMap();
        map.put("total",page.getTotal());
        map.put("list",page.getRecords());
        HttpResp httpResp = new HttpResp<>(ResultCode.OPTION_SUCCESS,new Date(),map);
        log.debug("httpResp:{}",httpResp);
        return httpResp;
    }
}
